//
//  WishInfoTableViewCell.m
//  LiuHeBao
//
//  Created by zr on 2018/8/22.
//  Copyright © 2018年 zr. All rights reserved.
//

#import "WishInfoTableViewCell.h"

@interface WishInfoTableViewCell ()

@property (nonatomic, weak)UIImageView * iconImageView;
@property (nonatomic, weak)UILabel * nameLabel;
@property (nonatomic, weak)UILabel * timeLabel;
@property (nonatomic, weak)UILabel * contentLabel;
@property (nonatomic, weak)UIView * lineView;

@end

@implementation WishInfoTableViewCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self setupUI];
    }
    
    return self;
}


- (void)setDict:(NSDictionary *)dict{

    _dict = dict;

    NSURL * url = [NSURL URLWithString:dict[@"user_headimg"]];
    [self.iconImageView sd_setImageWithURL:url placeholderImage:ImageNamed(@"weidenglu")];
    self.nameLabel.text = dict[@"user_name"];
    // timeStampString 是服务器返回的13位时间戳
    NSString *timeStampString  = dict[@"ctime"];

    // iOS 生成的时间戳是10位
    NSTimeInterval interval    =[timeStampString doubleValue];
    NSDate *date               = [NSDate dateWithTimeIntervalSince1970:interval];

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *dateString       = [formatter stringFromDate: date];
    self.timeLabel.text = dateString;
    self.contentLabel.text = dict[@"content"];
}

- (void)setupUI{
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.backgroundColor = WHITE_COLOR;
    
    UIImageView * iconImageView = [UIImageView new];
    
    //  把图片设置成圆形
    iconImageView.layer.cornerRadius = 20 * SCREENH6;//裁成圆角
    iconImageView.layer.masksToBounds = YES;//隐藏裁剪掉的部分
    iconImageView.backgroundColor = [UIColor grayColor];
        iconImageView.image = ImageNamed(@"weidenglu");
    
    self.iconImageView = iconImageView;
    
    [self.contentView addSubview:self.iconImageView];
    
    
    UILabel * nameLabel = [UILabel new];
    
    nameLabel.textColor = BLACK_COLOR;
    nameLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    nameLabel.text = @"用户姓名";
    
    self.nameLabel = nameLabel;
    
    [self.contentView addSubview:self.nameLabel];
    
    
    UILabel * contentLabel = [UILabel new];
    
    contentLabel.textColor = BLACK_COLOR;
    contentLabel.font = [UIFont systemFontOfSize:14 * SCREENH6];
    contentLabel.text = @"2desired background color to the backgroundView background color to the backgroundView to the backgroundView background color to the backgroundView";
    contentLabel.numberOfLines = 3;
    
    self.contentLabel = contentLabel;
    
    [self.contentView addSubview:self.contentLabel];
    
    
    UILabel * timeLabel = [UILabel new];
    
    timeLabel.textColor = [UIColor lightGrayColor];
    timeLabel.font = [UIFont systemFontOfSize:13 * SCREENH6];
    timeLabel.text = @"2018-07-24";
    
    self.timeLabel = timeLabel;
    
    [self.contentView addSubview:self.timeLabel];
    
    UIView * lineView = [UIView new];
    
    lineView.backgroundColor = ColorWithRGB(0xe6e6e6);
    
    self.lineView = lineView;
    
    [self.contentView addSubview:self.lineView];
}



- (void)layoutSubviews{
    
    [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.contentView).offset(12 * SCREENH6);
        make.top.mas_equalTo(self.contentView).offset(12 * SCREENH6);
        make.height.width.mas_equalTo(40 * SCREENH6);
    }];
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.iconImageView.mas_trailing).offset(6 * SCREENH6);
        make.centerY.mas_equalTo(self.iconImageView);
    }];
    
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.iconImageView);
        make.trailing.mas_equalTo(self.contentView).offset(-12 * SCREENH6);
        make.top.mas_equalTo(self.iconImageView.mas_bottom).offset(7 * SCREENH6);
    }];
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.contentLabel);
        make.top.mas_equalTo(self.contentLabel.mas_bottom).offset(10 * SCREENH6);
    }];
   
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.contentView).offset(12 * SCREENH6);
        make.trailing.mas_equalTo(self.contentView).offset(-12 * SCREENH6);
        make.height.mas_equalTo(1 * SCREENH6);
        make.bottom.mas_equalTo(self.contentView);
    }];
    
}


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
